package com.tech.connect.zhaofuwu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.image.ImageLoader;
import com.ksy.common.model.City;
import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.ProxyLayout;
import com.ksy.common.view.PullToRefreshLayout;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.CurrentInfo;
import com.tech.connect.api.ImageApi;
import com.tech.connect.api.LvXinHttp;
import com.tech.connect.api.UserInfo;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.model.ItemLvXin;
import com.tech.connect.model.PagerModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZhaoFuWuLvYouPeiFragment extends BaseFragment {

    private BaseAdapter<ItemLvXin, BaseHolder> adapter;
    private RecyclerView recycler;
    private ArrayList<ItemLvXin> allList = new ArrayList<>();
    private int height;
    private ProxyLayout<RecyclerView> proxyLayout;
    private int pageSize = 15;
    private int page = 1;
    protected Map<String, Object> optMap = new HashMap<>();
    private City selectCity;

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.layout_recycler, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            Serializable citySer = arguments.getSerializable("city");
            if (citySer != null && (citySer instanceof City)) {
                selectCity = (City) citySer;
            }
        }
        initView(view);
        loadData();
    }


    private void initView(View view) {
        int screenWidth = AndroidUtil.getScreenWidth(activity);
        height = (screenWidth - AndroidUtil.dip2px(50)) / 2;
        recycler = view.findViewById(R.id.recycler);
        proxyLayout = new ProxyLayout<>(activity, recycler);
        proxyLayout.setCanRefresh(true);
        proxyLayout.setDragListener(new PullToRefreshLayout.DragListener() {
            @Override
            public void drag2Upper(PullToRefreshLayout pullToRefreshLayout) {
                ++page;
                loadData();
            }

            @Override
            public void drag2Downer(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                loadData();
            }
        });
        recycler.setLayoutManager(new GridLayoutManager(activity, 2));
//        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(activity, BOTH_SET, 10, Color.parseColor("#000000"));
//        GridOffsetsItemDecoration itemDecoration = new GridOffsetsItemDecoration(GridOffsetsItemDecoration.GRID_OFFSETS_VERTICAL);
//        itemDecoration.setHorizontalItemOffsets(AndroidUtil.dip2px(10));
//        itemDecoration.setVerticalItemOffsets(AndroidUtil.dip2px(10));
//        recycler.addItemDecoration(dividerItemDecoration);
    }


    private void initAdapter(List<ItemLvXin> lvXinList) {
        if (lvXinList != null) {
            allList.addAll(lvXinList);
        }
        initAdapter();
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new BaseAdapter<ItemLvXin, BaseHolder>(R.layout.item_layout_lvxing_pei, allList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    ItemLvXin itemLvXin = allList.get(position);
                    TextView tvInfo = helper.getView(R.id.tvInfo);
                    TextView tvTitle = helper.getView(R.id.tvTitle);
                    ImageView ivImage = helper.getView(R.id.ivImage);
                    String imageCover = ImageApi.getImage(itemLvXin.images);
                    ImageLoader.getInstance().load(activity, imageCover, ivImage, new RequestOptions());
                    UserInfo user = itemLvXin.user;
                    if (user != null) {
                        tvTitle.setText(itemLvXin.user.nickName);
                        int age = user.age;
                        String provinceName = user.provinceName;
                        String cityName = user.cityName;
                        StringBuilder builder = new StringBuilder(String.valueOf(age)).append("岁");
//                        if (isNotEmpty(provinceName)) {
//                            builder.append(" ");
//                            builder.append(provinceName);
//                        }
                        if (isNotEmpty(cityName)) {
                            builder.append(" ");
                            builder.append(cityName);
                        }
                        tvInfo.setText(builder);
                    } else {
                        tvTitle.setText("");
                        tvInfo.setText("");
                    }
                }
            };
            adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(activity, ZhaoFuWuLvYouViewActivity.class);
                    intent.putExtra("dataList", allList);
                    intent.putExtra("index", position);
                    jump2Activity(intent);
                }
            });
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }


    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        if (selectCity != null) {
            map.put("cityCode", selectCity.id);
        }
        map.putAll(optMap);
        map.put("lat", CurrentInfo.getUserInfo().lat);
        map.put("lng", CurrentInfo.getUserInfo().lng);
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        LvXinHttp.lvxingPei(map, new BaseEntityOb<PagerModel<ItemLvXin>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemLvXin> data, String msg) {
                List<ItemLvXin> lvXinList = null;
                if (isSuccess && data != null) {
                    lvXinList = data.content;
                }
                if (page == 1) {
                    allList.clear();
                }
                initAdapter(lvXinList);
                if (lvXinList != null && lvXinList.size() >= pageSize) {
                    proxyLayout.setCanLoadMore(true);
                } else {
                    proxyLayout.setCanLoadMore(false);
                }
                if (allList.isEmpty()) {
                    proxyLayout.showEmptyView();
                } else {
                    proxyLayout.showContentView();
                }
                proxyLayout.dragFinish();
            }
        });
    }

    public void onOptClick(Map<String, Object> map) {
        //条件切换
        optMap.clear();
        optMap.putAll(map);
        loadData();

    }

    public void onCityClick(City city) {
        selectCity = city;
        loadData();
    }
}